using AvalonAdmin.Consts;
using AvalonAdmin.Helpers;
using AvalonAdmin.BaseViewModel;
using AvalonAdminDto.CarType.AreaDtos;
using AvalonAdminDto.Common;
using D.UtilCore.Dtos;
using System.Threading.Tasks;
using Avalonia.Threading;

namespace AvalonAdmin.Pages.CarType.ViewModels.Area
{
    /// <summary>
    /// 区域
    /// </summary>
    public class AreaModifyViewModel : BaseModifyViewModel
    {
        #region 页面属性及初始化数据

        private string? _id;

        public AreaModifyViewModel(string? id = null, ViewStatus? viewStatus = ViewStatus.Add) : base()
        {
            _id = id;
            SetupFormStatus(viewStatus);

            // 如果有ID，则加载数据
            if (!string.IsNullOrEmpty(_id))
            {
                _ = Task.Run(async () => await QueryAsync());
            }
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        public override async Task<bool> QueryAsync()
        {
            if (!string.IsNullOrEmpty(_id))
            {
                // 调用后端接口 
                var res = await HttpHelper<RepArea>.GetAsync($"{ApiUrl.GetArea}/{_id}");

                //  判断结果
                if (res.Code == StatusCodeEnum.Waring)
                {
                    ShowError(res.Msg);
                }
                else if (res.Code == StatusCodeEnum.Error)
                {
                    ShowError("服务器异常");
                }
                else
                {
                    // 绑定数据源到UI 
                    if (res.Data != null)
                    {
                        await Dispatcher.UIThread.InvokeAsync(() =>
                        {
                            Name = res.Data.Name;
                            Code = res.Data.Code;
                            EnglishName = res.Data.EnglishName;
                        });
                    }
                }
            }
            return true;
        }
        #endregion

        #region 数据编辑属性

        private string? name;
        /// <summary>
        /// 名称
        /// </summary>
        public string? Name
        {
            get => name;
            set => SetProperty(ref name, value);
        }

        private string? code;
        /// <summary>
        /// 编码
        /// </summary>
        public string? Code
        {
            get => code;
            set => SetProperty(ref code, value);
        }

        private string? englishName;
        /// <summary>
        /// 英文名
        /// </summary>
        public string? EnglishName
        {
            get => englishName;
            set => SetProperty(ref englishName, value);
        }

        #endregion

        #region 提交，重置
        /// <summary>
        /// 提交
        /// </summary>
        protected override async Task SubmitAsync()
        {
            // 参数验证判断
            if (string.IsNullOrEmpty(Name))
            {
                ShowWarning("请输入名称不能为空"); return;
            }

            // 根据状态判断
            if (string.IsNullOrEmpty(_id))
            {
                // 调用后端接口 
                var res = await HttpHelper<int>.PostAsync(ApiUrl.AddArea, new ReqAddArea()
                {
                    Name = Name,
                    Code = Code,
                    EnglishName = EnglishName
                });

                //  判断结果
                HandleApiResponse(res, "新增成功", "新增失败");
            }
            else
            {
                // 调用后端接口 
                var res = await HttpHelper<int>.PutAsync($"{ApiUrl.UpdateArea}", new ReqUpdateArea()
                {
                    Id = _id,
                    Name = Name,
                    Code = Code,
                    EnglishName = EnglishName
                });
                // 判断结果
                HandleApiResponse(res, "修改成功", "修改失败");
            }
        }
       
        /// <summary>
        /// 重置表单
        /// </summary>
        protected override void Reset()
        {
            Name = string.Empty;
            Code = string.Empty;
            EnglishName = string.Empty;
        }
        #endregion 
    }
}
